package study.linux.laptrinhlinux.mysql.source;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Inserter {
	public static int STRING = 0;
	public static int NUMBER = 1;

	public static void main(String[] args) throws Exception{
		Connection conn = null;
		Statement stmt = null;

		String comma = ",";
		int start = 7;
		int end = 50000;

		String schema = "fulltexttest";
		String table = "articles_innodb";
		String[] fields = {"title","body"};
		int [] desc = {STRING, STRING};

		StringBuilder insert = new StringBuilder("insert into " + table + "(");

		int fieldLength = fields.length;
		// (,a,b)
		for (int i = 0; i < fieldLength; i++) {
			insert.append(comma).append(fields[i]);
		}
		insert.append(")");
		insert.append(" values (");
		String prefix = insert.toString();
		prefix.replace("(,", ",");

		Class.forName("com.mysql.jdbc.Driver").newInstance();
		String connectionUrl = "jdbc:mysql://localhost3306/" + schema;
		String connectionUser = "nao";
		String connectionPassword = "nao";
		conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
		stmt = conn.createStatement();

		try {
			for (int i = start; i < end; i++) {
				StringBuilder insertBld = new StringBuilder(prefix);
				for (int j = 0; j < fieldLength; j++) {
					String f = fields[j];
					insertBld.append("," + ((desc[j] == STRING) ? ("'"+ (f + i) +"'") : (i)));
				}
				insertBld.append(")");
				// values(,a,1,2)
				String insertstm = insertBld.toString().replace("(,", "(");
				stmt.execute(insertstm);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

